
<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <el-form ref="queryForm" :model="queryParams" :inline="true">
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单编号" prop="order_number">
            <el-input
              v-model="queryParams.order_number"
              placeholder="请输入标题"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="提交时间" prop="time">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="small"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          :data="sysOrderList"
          border
          @selection-change="handleSelectionChange"
          @sort-change="handleSortChang"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" header-align="center" width="50" label="序号" fixed="left" align="center" />
          <el-table-column header-align="center" label="订单编号" fixed="left">
            <template slot-scope="scope">
              <span>{{ scope.row.order_number }}</span>
            </template>
          </el-table-column>
          <el-table-column header-align="center" prop="subject" label="科目" />
          <el-table-column header-align="center" prop="subject" label="课程名称">
            <template slot-scope="scope">
              <span>{{ scope.row.course ? scope.row.course.subject : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column header-align="center" prop="province" label="适用地区" />
          <el-table-column
            label="订单金额"
            header-align="center"
            align="left"
            prop="price"
            width="100"
          >
            <template slot-scope="scope">
              <span class="text-price">{{ $Prices(scope.row.price) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="订单返佣"
            header-align="center"
            align="left"
            prop="commission"
            width="100"
          >
            <template slot-scope="scope">
              <span class="text-price">{{ $Prices(scope.row.commission) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="wz_url"
            label="关联上级"
            header-align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.wz_url }}
            </template>
          </el-table-column>
          <el-table-column
            label="订单状态"
            header-align="center"
            align="left"
            prop="status"
          >
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 2 ? 'success' : 'danger'">
                {{ scope.row.status === 2 ? '已付款' : '待付款' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="买家信息"
            header-align="center"
            align="left"
            prop="buyer_id"
          >
            <template slot-scope="scope">
              <span>
                {{ scope.row.buyer_id }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="教师信息"
            header-align="center"
            align="left"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.teacher ? scope.row.teacher.nickName : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="提交时间"
            align="center"
            prop="createdAt"
            width="155px"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="120"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                v-permisaction="['admin:sysApi:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleReview(scope.row)"
              >复审
              </el-button>
              <el-button
                v-permisaction="['admin:sysApi:edit']"
                size="mini"
                type="text"
                icon="el-icon-view"
              >
                <router-link
                  :to="{
                    name: 'view',
                    params: {id: scope.row.id },
                    query: { type: 'offline' }
                  }"
                >查看</router-link>
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>

      <el-drawer
        ref="drawer"
        title="订单复审"
        :visible.sync="open"
        direction="rtl"
        size="40%"
        custom-class="demo-drawer"
      >
        <div class="info-body">
          <el-descriptions :column="2">
            <div v-show="info.order_number" slot="title">
              单号：{{ info.order_number }}
            </div>
            <el-descriptions-item label="购买人">{{ info.customer ? info.customer.name : '' }}</el-descriptions-item>
            <el-descriptions-item label="状态">
              <el-tag :type="info.status === 2 ? 'success' : 'danger'" size="small">
                {{ info.status === 2 ? '已付款' : '待付款' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="订单金额">
              <span class="text-price"> {{ $Prices(info.price) }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="购买课程">{{ info.subject }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{ info.createdAt && parseTime(info.createdAt) }}</el-descriptions-item>
            <el-descriptions-item label="联系电话">{{ info.customer ? info.customer.phone : '' }}</el-descriptions-item>
            <el-descriptions-item label="归属大区">{{ info.province }}</el-descriptions-item>
            <el-descriptions-item label="所属区域">苏州市</el-descriptions-item>
            <el-descriptions-item label="归属教学点">{{ info.subject }}</el-descriptions-item>
            <el-descriptions-item label="课程科目">{{ info.course ? info.course.name : '' }}</el-descriptions-item>
            <el-descriptions-item label="关联上级">{{ info.inviter ? info.inviter.name : '' }}</el-descriptions-item>
            <el-descriptions-item label="上级返佣">{{ $Prices(info.commission) }}</el-descriptions-item>
          </el-descriptions>
        </div>
        <el-form
          ref="form"
          :model="form"
          :rules="{ recheck: [{ required: true, message: '复审意见不能为空', trigger: 'blur' }] }"
          label-width="100px"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="跟进意见" prop="follow_up">
                <el-input
                  v-model="form.follow_up"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同编号" prop="contract_no">
                <el-input
                  v-model="form.contract_no"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="复审意见" prop="recheck">
                <el-input
                  v-model="form.recheck"
                  type="textarea"
                  :rows="3"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-drawer>
    </template>
  </BasicLayout>
</template>

<script>
import { offlineOrderList } from '@/api/admin/sys-order'
import { reviewOrder } from '@/api/admin/sys-finance'
export default {
  name: 'SysOrderReviewManage',
  components: {
  },
  data() {
    return {
      statusOptions: [
        { label: '待付款', value: 1 },
        { label: '已付款', value: 2 }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      form: {},
      info: {},
      sysOrderList: [],
      dateRange: [],
      logisticsList: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        status: undefined,
        begin_time: undefined,
        end_time: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true
      try {
        offlineOrderList(this.queryParams).then(response => {
          this.sysOrderList = response.data.list
          this.total = response.data.count
        }).finally(() => {
          this.loading = false
        })
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    parentIdFormat(row) {
      return this.selectItemsLabel(this.parentIdOptions, row.parentId)
    },
    // 文件
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.dateRange.forEach((item, index) => {
        if (index === 0) {
          this.queryParams.consult_wz_start = `${item} 00:00:00`
        } else {
          this.queryParams.consult_wz_end = `${item} 59:59:59`
        }
      })
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 排序回调函数 */
    handleSortChang(column, prop, order) {
      prop = column.prop
      order = column.order
      if (this.order !== '' && this.order !== prop + 'Order') {
        this.queryParams[this.order] = undefined
      }
      if (order === 'descending') {
        this.queryParams[prop + 'Order'] = 'desc'
        this.order = prop + 'Order'
      } else if (order === 'ascending') {
        this.queryParams[prop + 'Order'] = 'asc'
        this.order = prop + 'Order'
      } else {
        this.queryParams[prop + 'Order'] = undefined
      }
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.consult_wz_id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleReview(row) {
      this.reset()
      this.info = row
      this.form.id = row.id
      this.open = true
    },
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          reviewOrder(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.msg)
              this.open = false
              this.getList()
              this.form = {}
              this.info = {}
            } else {
              this.msgError(response.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .info-body {
    padding: 0 24px 16px;
  }
</style>
